/**
 * 
 */
package com.nari.heatshare.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.ftpclient.business.IRemoteFileManage;
import com.nari.heatshare.model.IAssignMeterDataFile;
import com.nari.heatshare.service.IAssignMeterDataFileService;
import com.nari.heatshare.utils.MeterDataTxtParser;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-18
 */
public class AssignMeterDataFileAction extends JSONActionSupport {
	private static final long serialVersionUID = -2298396616745431263L;
	private IAssignMeterDataFileService iAssignMeterDataFileService;
	//分配表数据文件查询
	private String fileName;
	private Date startDate;
	private Date endDate;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IAssignMeterDataFile> dataFilePage;
	//分配表数据文件信息管理
	private String dataFileUID;
	private IAssignMeterDataFile meterDataFile;
	//数据文件附件上传
	private File attachFile;
	private String attachFileFileName;
	//数据文件附件下载
	private InputStream targetFile;
	private String outputFileName;
	private String txtContent;
	private String detailType;
	//FTP远程文件下载及预览
	private IRemoteFileManage iRemoteFileManage;
	private String tempFilePath;
	
	/**
	 * 分页查询分配表数据文件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-1
	 */
	public String queryDataFilePage() throws Exception {
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.dataFilePage = this.iAssignMeterDataFileService.queryDataFilePage(fileName, startDate, endDate, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.dataFilePage.getPageCond().getTotal())
				.divide(new BigDecimal(this.dataFilePage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "dataFilePage";
	}
	
	/**
	 * 分配表数据文件上传初始化
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-1
	 */
	public String intialDataFileUpload() throws Exception {
		//暂无初始化数据
		return "dataFileUpload";
	}
	
	/**
	 * 上传并解析分配表数据文件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-1
	 */
	public String uploadMeterDataFile() throws Exception {
		this.rmsg = this.iAssignMeterDataFileService.saveDataFileUpload(attachFile, attachFileFileName);
		this.writeResponseToFileAsyncSubmit(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据唯一标识删除分配表数据文件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-1
	 */
	public String deleteDataFileByUID() throws Exception {
		this.rmsg = this.iAssignMeterDataFileService.deleteDataFileByUID(dataFileUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 下载分配表数据文件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-1
	 */
	public String downloadAttachFile() throws Exception {
		this.meterDataFile = this.iAssignMeterDataFileService.queryEntityById(dataFileUID);
		//文件下载时显示名称
		this.outputFileName=new String(meterDataFile.getFileName().getBytes(),"ISO-8859-1");
		//下载FTP远程文件并构造输出流
		String saveFileName = meterDataFile.getSaveUrl().substring(meterDataFile.getSaveUrl().lastIndexOf("/")+1);
		this.rmsg = iRemoteFileManage.downloadFile("", saveFileName, this.tempFilePath);
		if(rmsg.isSuccess()) {
			this.setTargetFile(new FileInputStream(this.tempFilePath + "/" + saveFileName));
		}
//		this.targetFile = new FileInputStream(new File(meterDataFile.getDiskID()+"/"+meterDataFile.getSaveUrl()));
		return "downloadFile";
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-29
	 */
	public String queryUploadDetail() throws Exception {
		if(dataFileUID!=null && !"".equals(dataFileUID)) {
			this.meterDataFile = iAssignMeterDataFileService.queryEntityById(dataFileUID);
			//查询上传的TXT文件内容
			if("txtContent".equals(detailType)) {
				//下载FTP远程文件
				String saveFileName = meterDataFile.getSaveUrl().substring(meterDataFile.getSaveUrl().lastIndexOf("/")+1);
				this.rmsg = iRemoteFileManage.downloadFile("", saveFileName, this.tempFilePath);
				if(rmsg.isSuccess()) {
					this.txtContent = MeterDataTxtParser.readTxtContent(new File(this.tempFilePath + "/" + saveFileName));
				}
//				this.txtContent = MeterDataTxtParser.readTxtContent(meterDataFile);
				return "txtContentDetail";
			}
		}
		return "txtUploadDetail";
	}
	
	/**
	 * @return the iAssignMeterDataFileService
	 */
	@JSON(serialize=false)
	public IAssignMeterDataFileService getiAssignMeterDataFileService() {
		return iAssignMeterDataFileService;
	}
	
	/**
	 * @param iAssignMeterDataFileService the iAssignMeterDataFileService to set
	 */
	public void setiAssignMeterDataFileService(
			IAssignMeterDataFileService iAssignMeterDataFileService) {
		this.iAssignMeterDataFileService = iAssignMeterDataFileService;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param fileName the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the dataFilePage
	 */
	public ResultWithPageCond<IAssignMeterDataFile> getDataFilePage() {
		return dataFilePage;
	}

	/**
	 * @param dataFilePage the dataFilePage to set
	 */
	public void setDataFilePage(
			ResultWithPageCond<IAssignMeterDataFile> dataFilePage) {
		this.dataFilePage = dataFilePage;
	}

	/**
	 * @return the dataFileUID
	 */
	public String getDataFileUID() {
		return dataFileUID;
	}

	/**
	 * @param dataFileUID the dataFileUID to set
	 */
	public void setDataFileUID(String dataFileUID) {
		this.dataFileUID = dataFileUID;
	}

	/**
	 * @return the meterDataFile
	 */
	public IAssignMeterDataFile getMeterDataFile() {
		return meterDataFile;
	}

	/**
	 * @param meterDataFile the meterDataFile to set
	 */
	public void setMeterDataFile(IAssignMeterDataFile meterDataFile) {
		this.meterDataFile = meterDataFile;
	}

	/**
	 * @return the attachFile
	 */
	public File getAttachFile() {
		return attachFile;
	}

	/**
	 * @param attachFile the attachFile to set
	 */
	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return the attachFileFileName
	 */
	public String getAttachFileFileName() {
		return attachFileFileName;
	}

	/**
	 * @param attachFileFileName the attachFileFileName to set
	 */
	public void setAttachFileFileName(String attachFileFileName) {
		this.attachFileFileName = attachFileFileName;
	}

	/**
	 * @return the targetFile
	 */
	public InputStream getTargetFile() {
		return targetFile;
	}

	/**
	 * @param targetFile the targetFile to set
	 */
	public void setTargetFile(InputStream targetFile) {
		this.targetFile = targetFile;
	}

	/**
	 * @return the outputFileName
	 */
	public String getOutputFileName() {
		return outputFileName;
	}

	/**
	 * @param outputFileName the outputFileName to set
	 */
	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}

	/**
	 * @return the txtContent
	 */
	public String getTxtContent() {
		return txtContent;
	}

	/**
	 * @param txtContent the txtContent to set
	 */
	public void setTxtContent(String txtContent) {
		this.txtContent = txtContent;
	}

	/**
	 * @return the detailType
	 */
	public String getDetailType() {
		return detailType;
	}

	/**
	 * @param detailType the detailType to set
	 */
	public void setDetailType(String detailType) {
		this.detailType = detailType;
	}

	/**
	 * @return the iRemoteFileManage
	 */
	public IRemoteFileManage getiRemoteFileManage() {
		return iRemoteFileManage;
	}

	/**
	 * @param iRemoteFileManage the iRemoteFileManage to set
	 */
	public void setiRemoteFileManage(IRemoteFileManage iRemoteFileManage) {
		this.iRemoteFileManage = iRemoteFileManage;
	}

	/**
	 * @return the tempFilePath
	 */
	public String getTempFilePath() {
		return tempFilePath;
	}

	/**
	 * @param tempFilePath the tempFilePath to set
	 */
	public void setTempFilePath(String tempFilePath) {
		this.tempFilePath = tempFilePath;
	}
	
}
